<?php
namespace app\models;

use yii;
use yii\db\Query;

class Product
{
    private $table;
    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix.'product';
    }

    public function insertRecord($data)
    {
        yii::$app->db->createCommand()->insert($this->table,$data)->execute();
    }

    public function get_product_list($p, $s, $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['invoice']))
            $db->andWhere(['in','invoice',$where['invoice']]);

        $list = $db->orderBy(['invoice'=>SORT_DESC,'date'=>SORT_DESC])
        ->offset(($p-1)*$s)
        ->limit($s)
        ->all();
        return $list;
    }

    public function get_product_size($where = [])
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if (!empty($where['invoice']))
            $db->andWhere(['in','invoice',$where['invoice']]);

        $info = $db->one();
        return $info?$info['num']:0;
    }

    public function get_invoice_product($invoice)
    {
        $db = new Query();
        return $db->select('*')->from($this->table)->where(['invoice'=>$invoice])->all();
    }

    public function deleteRecordByInvoice($in)
    {
        return yii::$app->db->createCommand()->delete($this->table,['invoice'=>$in])->execute();
    }
}
